// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.linqingying.gitcode.pullrequest.data.provider

import com.intellij.openapi.Disposable
import com.intellij.openapi.util.Disposer
import com.intellij.util.concurrency.annotations.RequiresEdt
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import com.linqingying.gitcode.api.data.pullrequest.GCPullRequestFileViewedState
import java.util.concurrent.CompletableFuture

interface GCPRViewedStateDataProvider {

  @RequiresEdt
  fun loadViewedState(): CompletableFuture<Map<String, GCPullRequestFileViewedState>>

  @RequiresEdt
  fun getViewedState(): Map<String, GCPullRequestFileViewedState>?

  @RequiresEdt
  fun updateViewedState(path: String, isViewed: Boolean)

  @RequiresEdt
  fun addViewedStateListener(parent: Disposable, listener: () -> Unit)

  @RequiresEdt
  fun reset()
}

fun GCPRViewedStateDataProvider.createViewedStateRequestsFlow(): Flow<CompletableFuture<Map<String, GCPullRequestFileViewedState>>> =
  callbackFlow {
    val disposable = Disposer.newDisposable()
    addViewedStateListener(disposable) {
      trySend(loadViewedState())
    }
    send(loadViewedState())
    awaitClose { Disposer.dispose(disposable) }
  }
